package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.OrganizationDao;
import mil.af.amc.adpetracker.model.Organization;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import javax.persistence.NonUniqueResultException;

/**
 * Please Fill in this Javadoc
 *
 * @author Jason Ferguson
 */
@Repository("organizationDao")
public class HibernateOrganizationDao extends HibernateGenericDao<Organization> implements OrganizationDao {

    private static final Log log = LogFactory.getLog(HibernateOrganizationDao.class);

    public HibernateOrganizationDao() {
        super(Organization.class);
    }

    public Organization getByOrganizationName(final String name) {

        Criteria orgCriteria = sessionFactory.getCurrentSession().createCriteria(Organization.class);
        orgCriteria.add(Restrictions.eq("name", name.toUpperCase()));

        Organization result = null;
        try {
            result = (Organization) orgCriteria.uniqueResult();
        } catch (NonUniqueResultException e) {
            log.debug("getByOrganization() throw NonUniqueResultException", e);
        }
        catch (HibernateException e) {
            log.debug("getByOrganization() threw HibernateException, may have returned > 1 result",e);
        }
        return result;
//        
//        return (Organization) getHibernateTemplate().execute(new HibernateCallback() {
//            public Object doInHibernate(Session session) throws HibernateException, SQLException {
//
//                Criteria crit = session.createCriteria(Organization.class);
//                crit.add(Restrictions.eq("name", name.toUpperCase()));
//                return crit.uniqueResult();
//            }
//        });

    }
}
